package com.ztom.daily.n2022.m6;

/**
 * 爱吃香蕉的珂珂
 * <p>
 * https://leetcode.cn/problems/koko-eating-bananas/
 *
 * @author Zhangtao
 */
public class Day07MinEatingSpeed {

    public int minEatingSpeed(int[] piles, int h) {
        int max = 0;
        for (int pile : piles) {
            max = Math.max(max, pile);
        }

        int l = 1;
        int r = max;
        while (l < r) {
            int speed = l + ((r - l) >> 1);
            long time = getTime(piles, speed);
            if (time <= h) {
                r = speed;
            } else {
                l = speed + 1;
            }
        }
        return r;
    }

    private long getTime(int[] piles, int speed) {
        long time = 0;
        for (int pile : piles) {
            // 向上取整
            time += (pile + speed - 1) / speed;
        }
        return time;
    }
}
